<% @regions.each do |r| 
  next if Log.where("weight is not null and region_id = #{r.id}").count <= 0
%>
<h2><%= r.name %></h2><br>
<% 
  pounds_per_year = {}
  pounds_per_month = {}
  first_recorded_pickup = nil
  Log.where("weight is not null and region_id = #{r.id}").each{ |l|
    pounds_per_year[l.when.year] = 0 if pounds_per_year[l.when.year].nil?
    pounds_per_year[l.when.year] += l.weight
    mokey = l.when.strftime("%Y-%m")
    pounds_per_month[mokey] = 0 if pounds_per_month[mokey].nil?
    pounds_per_month[mokey] += l.weight
    first_recorded_pickup = l.when if first_recorded_pickup.nil? or l.when < first_recorded_pickup
  }
%>
Before <%= first_recorded_pickup.to_s %>: <%= r.prior_lbs_rescued %><br>
<% 
   n = 0
   pounds_per_year.keys.sort.each{ |year| %>
     <%= n == 0 ? "Remainder of " : "" %><%= year %><%= n == pounds_per_year.length-1 ? " YTD" : "" %>: <%= pounds_per_year[year] %><br>   
<%   n += 1
   }
   per_year_flat =  pounds_per_year.keys.sort.collect{ |y| pounds_per_year[y].to_f } 
   food_chart_year = LazyHighCharts::HighChart.new('column') do |f|
     f.options[:chart][:defaultSeriesType] = "column"
     f.options[:chart][:plotBackgroundColor] = nil
     f.options[:title][:text] = "Food Rescued By Year"
       f.options[:xAxis] = {
       :plot_bands => "none",
       :title=>{:text=>"Year"},
       :categories => pounds_per_year.keys.sort}
    f.options[:yAxis][:title][:text] = "lbs of food"
    f.series(:name=>'Pounds of Food Rescued', :data=> per_year_flat)
  end
  per_month_flat = pounds_per_month.keys.sort.collect{ |y| pounds_per_month[y].to_f }
  food_chart_month = LazyHighCharts::HighChart.new('graph') do |f|
     f.options[:chart][:defaultSeriesType] = "area"
     f.options[:chart][:plotBackgroundColor] = nil
     f.options[:title][:text] = "Food Rescued By Month"
       f.options[:xAxis] = {
       :plot_bands => "none",
       :title=>{:text=>"Year"},
       :categories => pounds_per_month.keys.sort}
    f.options[:yAxis][:title][:text] = "lbs of food"
    f.series(:name=>'Pounds of Food Rescued', :data=> per_month_flat)
  end

%>
   <div style="width: 500px;"><%= high_chart("chart_food_per_year_#{r.id}", food_chart_year)  %></div>
   <div style="width: 500px;"><%= high_chart("chart_food_per_month_#{r.id}", food_chart_month)  %></div>
   <% if r.id == 1 %>
   <%= gmaps(:map_options => { :id => "map_#{r.id}" },
             :markers => { "data" => Location.where("region_id" => r.id).to_gmaps4rails }) %>
   <% end %>
   <br><br>

<% end %>
